//===-- Version.cpp - BiShengIR Version Number ----------------------------===//
//
// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//===----------------------------------------------------------------------===//
//
// This file defines several version-related utility functions for BiShengIR.
//
//===----------------------------------------------------------------------===//

#include "bishengir/Version/Version.h"
#include "bishengir/Config/bishengir-config.h"
#include "bishengir/Version/Version.inc"
#include "llvm/Support/raw_ostream.h"

#include "VCSVersion.inc"

namespace bishengir {

/// automatically generated by GenerateVersionFromVCS.cmake
std::string getBiShengIRRepositoryPath() {
#ifdef BISHENGIR_REPOSITORY
  return BISHENGIR_REPOSITORY;
#else
  return "";
#endif
}

std::string getLLVMRepositoryPath() {
#ifdef LLVM_REPOSITORY
  return LLVM_REPOSITORY;
#else
  return "";
#endif
}

/// automatically generated by GenerateVersionFromVCS.cmake
std::string getBiShengIRRevision() {
#ifdef BISHENGIR_REVISION
  return BISHENGIR_REVISION;
#else
  return "";
#endif
}

std::string getLLVMRevision() {
#ifdef LLVM_REVISION
  return LLVM_REVISION;
#else
  return "";
#endif
}

std::string getBiShengIRVendor() {
#ifdef BISHENGIR_VENDOR
  return BISHENGIR_VENDOR;
#else
  return "";
#endif
}

std::string getBiShengIRFullRepositoryVersion() {
  std::string buf;
  llvm::raw_string_ostream OS(buf);
  std::string Path = getBiShengIRRepositoryPath();
  std::string Revision = getBiShengIRRevision();
  if (!Path.empty() || !Revision.empty()) {
    OS << '(';
    if (!Path.empty())
      OS << Path;
    if (!Revision.empty()) {
      if (!Path.empty())
        OS << ' ';
      OS << Revision;
    }
    OS << ' ' << BISHENGIR_BUILD_DATE << ')';
  }
  // Support LLVM in a separate repository.
  std::string LLVMRev = getLLVMRevision();
  if (!LLVMRev.empty() && LLVMRev != Revision) {
    OS << " (";
    std::string LLVMRepo = getLLVMRepositoryPath();
    if (!LLVMRepo.empty())
      OS << LLVMRepo << ' ';
    OS << LLVMRev << ')';
  }
  return buf;
}

std::string getBiShengIRFullVersion() {
  return getBiShengIRToolFullVersion("bishengir");
}

/// Like getBiShengIRFullVersion(), but with a custom tool name.
std::string getBiShengIRToolFullVersion(llvm::StringRef ToolName) {
  std::string buf;
  llvm::raw_string_ostream OS(buf);
  OS << ToolName;
  std::string vendor = getBiShengIRVendor();
  if (!vendor.empty()) {
    OS << " " << vendor;
  }
  OS << " version " BISHENGIR_VERSION_STRING;

  std::string repo = getBiShengIRFullRepositoryVersion();
  if (!repo.empty()) {
    OS << " " << repo;
  }

#if BISHENGIR_IS_DEBUG_BUILD
  OS << "\nDEBUG build";
#else
  OS << "\nOptimized build";
#endif
#ifndef NDEBUG
  OS << " with assertions.";
#endif
  OS << "\n";

  return buf;
}

} // namespace bishengir
